package com.starchen.optimalselection.product.controller;

import com.starchen.optimalselection.common.utils.Result;
import com.starchen.optimalselection.common.valid.AddGroup;
import com.starchen.optimalselection.common.valid.UpdateGroup;
import com.starchen.optimalselection.common.service.CategoryService;
import com.starchen.optimalselection.common.model.Category;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品分类管理")
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @ApiOperation("查询所有商品分类 树形结构")
    @GetMapping("/treelist")
    public Result treelist(){
        List<Category> entities = categoryService.getTreeList();
        return Result.success(entities);
    }

    @ApiOperation("添加产品分类")
    @PostMapping("/create")
    public Result save(@Validated({AddGroup.class}) @RequestBody Category category){
        categoryService.save(category);
        return Result.success();
    }

    @ApiOperation("根据id获取商品分类")
    @GetMapping("/info")
    public Result info(Long id){
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    @ApiOperation("修改商品分类")
    @PostMapping("/update")
    public Result update(@Validated({UpdateGroup.class}) @RequestBody Category category){
        categoryService.updateById(category);
        return Result.success();
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品分类")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id){
        categoryService.removeById(id);
        return Result.success();
    }

}
